package com.runjian.rundo.iot.agent.business.publisher.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.runjian.rundo.iot.agent.business.publisher.DeviceMessageProcessor;
import com.runjian.rundo.iot.agent.business.publisher.MessagePublisher;
import com.runjian.rundo.iot.agent.common.enums.DeviceMessageType;
import com.runjian.rundo.iot.agent.common.message.DecodedMessage;
import com.runjian.rundo.iot.agent.common.message.DeviceMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 设备属性消息处理器
 *
 * @author LiGang
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class VppPropertiesMessageProcessor implements DeviceMessageProcessor<DeviceMessage> {

	private final MessagePublisher mqMessagePublisher;

	@Override
	public void process(DeviceMessage deviceMessage) {
		for (DecodedMessage decodedMessage : deviceMessage.getDecodedMessages()) {
			if (ObjectUtil.isNotEmpty(decodedMessage.getPayload())) {
				mqMessagePublisher.sendVppMessage(decodedMessage);
			} else {
				if (log.isDebugEnabled()) {
					log.debug("设备属性消息为空,设备Id:{},网关Id:{},解析数据:{}", decodedMessage.getDeviceId(), decodedMessage.getGatewayId(), decodedMessage);
				}
			}
		}
	}

	@Override
	public DeviceMessageType getType() {
		return DeviceMessageType.VPP_INSTRUCTION_MESSAGE;
	}
}
